package com.anosine.gpsl.android.util;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anosine.gpsl.android.R;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
/**  
 * @Title:  MyExpandableListAdapter  
 * @Package com.anosine.gpsl.android.util
 * @Description: 
 * @author: sunny     
 * @date:   2015-11-24
 * @version V1.0     
 */  
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	private Context context;
	private ArrayList<MKOLSearchRecord> records;

	public MyExpandableListAdapter(Context context, ArrayList<MKOLSearchRecord> records) {
		this.context = context;
		this.records = records;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return records.get(groupPosition).childCities.get(childPosition);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		RelativeLayout childs = (RelativeLayout) View.inflate(context, R.layout.offline_childs, null);
		TextView text_name = (TextView) childs.findViewById(R.id.text_name);
		TextView text_size = (TextView) childs.findViewById(R.id.text_size);
		if (records.get(groupPosition).childCities != null) {
			text_name.setText(records.get(groupPosition).childCities.get(childPosition).cityName);
			text_size.setText(String.format("%.1fM", records.get(groupPosition).childCities.get(childPosition).size
					/ (1024 * 1024.0)));
		}
		return childs;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (records.get(groupPosition).childCities.size() > 0) {
			return records.get(groupPosition).childCities.size();
		}
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return records.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return records.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		RelativeLayout groups = (RelativeLayout) View.inflate(context, R.layout.offline_groups, null);
		TextView text_name = (TextView) groups.findViewById(R.id.text_name);
		TextView text_size = (TextView) groups.findViewById(R.id.text_size);
		ImageView img_arrow = (ImageView) groups.findViewById(R.id.img_arrow);
		text_name.setText(records.get(groupPosition).cityName);
		if (records.get(groupPosition).childCities == null) {
			text_size.setText(String.format("%.1fM", records.get(groupPosition).size / (1024 * 1024.0)));
			text_size.setVisibility(View.VISIBLE);
			img_arrow.setVisibility(View.GONE);
		} else {
			text_size.setText("");
			text_size.setVisibility(View.GONE);
			img_arrow.setVisibility(View.VISIBLE);
		}
		// 判断isExpanded就可以控制是按下还是关闭，同时更换图片
		if (isExpanded) {
			img_arrow.setBackgroundResource(R.drawable.arrow_up);
		} else {
			img_arrow.setBackgroundResource(R.drawable.arrow_down);
		}
		return groups;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

}
